package conf

import (
	"blockchains-aio/util"
	"fmt"
	"strings"
)

const (
	BrindDownContainersInCompose = iota + 10000
)

var (
	DefaultComposeFileName = "compose.yaml"
	RootGroup              = "/blockchainsaio"

	Options = SysteOptions{
		Port:                   39781,
		Home:                   "~/.blockchains_aio",
		ContractRep:            "/contracts",
		ContractRepTemplates:   "/template",
		ContractRepIndividuals: "/individuals",
		StackRep:               "/stack",
		StackTemplate:          "/tmpl",
		StackTemplateRes:       "/res",     //where the stack config yaml template is
		StackConfig:            "/runtime", //where the stack yaml runtimely is
		Nfs:                    "/nfs",
		Ca:                     "/cac",

		Organizations: "/organizations",
		Ns3:           "/ns3",
	}

	CompiledDate string
)

type SysteOptions struct {
	Port                   int
	Home                   string
	ContractRep            string
	ContractRepTemplates   string
	ContractRepIndividuals string
	StackRep               string
	StackTemplate          string
	StackTemplateRes       string
	StackConfig            string
	Nfs                    string
	Ca                     string

	Organizations string
	Ns3           string
}

func GetContractRepTemplatesFolder() string {
	p, _ := util.ExpandTilde(fmt.Sprintf("%s%s%s", Options.Home, Options.ContractRep, Options.ContractRepTemplates))
	return p
}

func GetContractRepIndividualsFolder() string {
	p, _ := util.ExpandTilde(fmt.Sprintf("%s%s%s", Options.Home, Options.ContractRep, Options.ContractRepIndividuals))
	return p
}

func GetStackTemplateFolder() string {
	p, _ := util.ExpandTilde(fmt.Sprintf("%s%s%s", Options.Home, Options.StackRep, Options.StackTemplate))
	return p
}

func GetStackTemplateResFolder(consensus string) string {
	if len(consensus) == 0 {
		consensus = "poa"
	}

	p, _ := util.ExpandTilde(fmt.Sprintf("%s%s%s%s%s", Options.Home, Options.StackRep, Options.StackTemplate, Options.StackTemplateRes, "/"+consensus))
	return p
}

func GetStackTemplateResGethFolder(consensus string) string {
	p, _ := util.ExpandTilde(fmt.Sprintf("%s/%s", GetStackTemplateResFolder(consensus), "geth"))
	return p
}

func GetStackTemplateResDir(consensus, folderUnderRes string) string {
	p, _ := util.ExpandTilde(fmt.Sprintf("%s/%s", GetStackTemplateResFolder(consensus), folderUnderRes))
	return p
}

func GetStackConfigFolder() string {
	p, _ := util.ExpandTilde(fmt.Sprintf("%s%s%s", Options.Home, Options.StackRep, Options.StackConfig))
	return p
}

func GetNfsFolder() string {
	p, _ := util.ExpandTilde(fmt.Sprintf("%s%s", Options.Home, Options.Nfs))
	return p
}

func GetCacFolder() string {
	p, _ := util.ExpandTilde(fmt.Sprintf("%s%s", Options.Home, Options.Ca))
	return p
}

func GetGenesisTemplateFolder(consensus string) string {
	if consensus == "poa2" || consensus == "pow2" {
		return GetStackTemplateResDir(consensus, "config")
	}

	return ""
}

// optional value: orgcpp.zhaowu.com, orggo.zhaowu.com
func GetPeerOrganization(orgName string) string {
	if !strings.HasPrefix(orgName, "/") {
		orgName = "/" + orgName
	}
	p, _ := util.ExpandTilde(fmt.Sprintf("%s%s%s%s", Options.Home, Options.Organizations, "/peerOrganizations", orgName))
	return p
}

func GetNs3() string {
	p, _ := util.ExpandTilde(fmt.Sprintf("%s%s", Options.Home, Options.Ns3))
	return p
}
